<template>
  <div class="inviteList">
    <h3>邀请列表</h3>
    <div class="allTotal" v-if="initData.invite_all">
      <span>参与邀请人数：{{initData.invite_all.id_all}}</span>
      <span>被邀请人数：{{initData.invite_all.invite_all}}</span>
      <span>平均每人邀请数：{{initData.invite_all.average}}</span>
      <span>总业绩：{{initData.invite_all.bill}}</span>
      <span>总收益：{{initData.invite_all.amount}}</span>
    </div>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入用户名" v-model="search1">
          <template slot="prepend">用户名</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入用户id" v-model="search2">
          <template slot="prepend">用户id</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入手机号码" v-model="search3">
          <template slot="prepend">手机号码</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入邀请码" v-model="search4">
          <template slot="prepend">邀请码</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>注册时间</label>
        <el-date-picker
          v-model="search5"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="nick" label="用户名"></el-table-column>
        <el-table-column header-align="center" align="center" prop="id" label="用户Id" width="110"></el-table-column>
        <el-table-column header-align="center" align="center" prop="mobile" label="手机号" width="200"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="status_text"
          label="账户类型"
          width="130"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="device_text"
          label="设备"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="ip"
          label="IP"
          width="100"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="code" label="邀请码"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="invite"
          label="邀请数量"
          width="80"
        ></el-table-column>
         <el-table-column
          header-align="center"
          align="center"
          prop="pay_child_count"
          label="付费人数"
          width="80"
        >
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="累计业绩"
          width="80"
        >
          <template slot-scope="scope">
            <span>{{scope.row.agent?scope.row.agent.bill:0}}</span>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="agent?agent.balance:0"
          label="收益余额"
          width="80"
        >
         <template slot-scope="scope">
            <span>{{scope.row.agent?scope.row.agent.balance:0}}</span>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="邀请列表"
          fixed="right"
          width="150"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="success" @click="handleView(scope.$index, scope.row)" v-if="permissions_obj.id_98||permissions_obj.id_all">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"       
        :page-size="pageSize1"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize1"
      ></el-pagination>
    </div>
    <!-- 查看详情 -->
    <el-dialog title="邀请列表" :visible.sync="centerDialogVisible" width="50%" center>
      <div class="detail" v-if="detailData.user_info">
        <span>用户名：{{detailData.user_info.nick}}</span>
        <span>用户ID：{{detailData.user_info.id}}</span>
        <span>注册时间：{{detailData.user_info.created_at}}</span>
        <span>邀请人数：{{detailData.user_info.invite}}</span>
      </div>
      <div>1级用户 人数：{{detailData.user_info&&detailData.user_info.agent?detailData.user_info.agent.a_count:0}} 业绩：{{detailData.user_info&&detailData.user_info.agent?detailData.user_info.agent.a_bill:0}}</div>
      <div>2级用户 人数：{{detailData.user_info&&detailData.user_info.agent?detailData.user_info.agent.b_count:0}} 业绩：{{detailData.user_info&&detailData.user_info.agent?detailData.user_info.agent.b_bill:0}}</div>
      <div>3级用户 人数：{{detailData.user_info&&detailData.user_info.agent?detailData.user_info.agent.c_count:0}} 业绩：{{detailData.user_info&&detailData.user_info.agent?detailData.user_info.agent.c_bill:0}}</div>
      <div class="dialogList">
        <el-table
          border
          ref="multipleTable"
          :data="dialogTableData"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column header-align="center" align="center" prop="nick" label="用户名"></el-table-column>
          <el-table-column header-align="center" align="center" prop="id" label="用户Id"></el-table-column>
          <el-table-column header-align="center" align="center" prop="mobile" label="手机号"></el-table-column>
          <el-table-column header-align="center" align="center" prop="ip" label="IP"></el-table-column>
          <el-table-column header-align="center" align="center" prop="area" label="地区"></el-table-column>
          <el-table-column header-align="center" align="center" prop="device_text" label="设备"></el-table-column>
          <el-table-column header-align="center" align="center" prop="order_count" label="订单数量"></el-table-column>
          <el-table-column header-align="center" align="center" prop="pay_count" label="充值数量"></el-table-column>
          <el-table-column header-align="center" align="center" prop="created_at" label="邀请时间"></el-table-column>
        </el-table>  
      </div>
      <div class="page">
        <el-pagination
          background
          @current-change="handleCurrentChange2"
          :current-page.sync="currentPage2"
          :page-size="pageSize2"
          layout="total, prev, pager, next, jumper"
          :total="totalSize2"
        ></el-pagination>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import QRCode  from "qrcodejs2"
export default {
  name: "inviteList",
  data() {
    return {
      search1: "",
      search2: "",
      search3: "",
      search4: "",
      search5: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      //表格数据
      tableData: [],
      currentPage1: 1,
      pageSize1: 10,
      totalSize1: 0,
      centerDialogVisible: false,
      currentPage2: 1,
      pageSize2: 6,
      totalSize2: 0,
      //弹窗表格数据
      dialogTableData: [],
      initData: {},
      detailData: {},
      selectId: "",
      // 权限管理
       permissions:[],
      permissions_obj:{},
    };
  },
  created(){ 
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff=false;
    this.permissions.forEach((item, index) => {
      if(item==-1){
        oneOff=true;
      }else{
        this.permissions_obj['id_'+item]=item;
      }
    });
    if(oneOff){
      this.permissions_obj.id_all=true;
    }else{
      this.permissions_obj.id_all=false;
    };
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      this.currentPage1 = val;
      this.getDataList();
    },
      handleSizeChange(val){
      this.pageSize1=val;
      this.getDataList();
    },
    searchMethod(){
      this.currentPage1 = 1;
      this.getDataList();
    },
    handleView(index, row) {
      this.centerDialogVisible = true;
      this.selectId = row.id;
      this.getUserInviteDetail(this.selectId);
    },
    //弹窗分页
    handleCurrentChange2(val) {
      this.currentPage2 = val;
      this.getUserInviteDetail(this.selectId);
    },
    //获取初始化邀请列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize1,
          search: {
            nick: this.search1,
            id: this.search2,
            mobile: this.search3,
            code: this.search4,
            stime: this.search5&&this.search5.length > 0 ? this.search5[0] : "",
            otime: this.search5&&this.search5.length > 1 ? this.search5[1] : ""
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.userInvite_list,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.invite_list.data.forEach((item, index) => {
          if (item.status == 0) {
            item.status_text = "禁用";
          } else if (item.status == 1) {
            item.status_text = "正常";
          } else if (item.status == 2) {
            item.status_text = "删除";
          }
          if (item.device_type == "I") {
            item.device_text = "苹果";
          } else if (item.device_type == "A") {
            item.device_text = "安卓";
          } else if (item.device_type == "H") {
            item.device_text = "来源h5";
          }
        });
        this.totalSize1 = this.initData.invite_list.total;
        this.tableData = this.initData.invite_list.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取用户邀请详情
    async getUserInviteDetail(id) {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage2,
          page_size: this.pageSize2,
          id: id
        }
      };
      let res = await this.$axios.post(
        this.$api.userInvite_detail,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.detailData = res.data;
         this.detailData.invite_list.data.forEach((item, index) => {
          if (item.device_type == "I") {
            item.device_text = "苹果";
          } else if (item.device_type == "A") {
            item.device_text = "安卓";
          } else if (item.device_type == "H") {
            item.device_text = "来源h5";
          }
        });
        this.totalSize2 = this.detailData.invite_list.total;
        this.dialogTableData = this.detailData.invite_list.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.detail {
  margin-bottom: 0.2rem;
}
.detail span {
  margin-right: 0.1rem;
}
.allTotal {
  padding: 0 0.2rem;
}
.allTotal span {
  margin-right: 0.3rem;
}
</style>